1 package org.naftulin.classpathexplorer.dublicate.imlp;
2
3 import java.io.File;
4 import java.util.LinkedList;
5 import java.util.List;
6
7 import org.naftulin.classpathexplorer.AccessibleResource;
8 import org.naftulin.logwrapper.LogAdapter;
9 import org.naftulin.logwrapper.LogLevelAdaptor;
10 import org.naftulin.timespan.TimeSpan;
11
12
13 /***
14 * Represents a directory - archive for accessible resources.
15 * @author henry naftulin
16 * @version 1.0
17 */
18 class DirectoryArchive extends AccessibleArchive {
19 private static LogAdapter log = LogAdapter.getLogger(DirectoryArchive.class);
20
21 DirectoryArchive(File file) {
22 super(file);
23 }
24
25 /***
26 * Returns all of the resources in this directory.
27 * @return all of the resources in this directory.
28 */
29 public AccessibleResource[] getAccessibleResources() {
30 TimeSpan readTime = new TimeSpan("reading resource from " + getPath());
31 readTime.start();
32 File file = new File(getPath());
33 List resources = new LinkedList();
34 String rootPath = "";
35 if (file.isDirectory()) {
36 addResourcesFromSubDirectory(file, resources, rootPath);
37 } else {
38
39 log.log(LogLevelAdaptor.ERROR, "no resources found, path is not a directory " + getPath());
40 }
41
42
43 AccessibleResource[] accessibleResources = new AccessibleResource[resources.size()];
44 accessibleResources = (AccessibleResource[]) resources.toArray(accessibleResources);
45 readTime.stop();
46 readTime.log(log, LogLevelAdaptor.DEBUG);
47 return accessibleResources;
48 }
49
50 /***
51 * Either adds a resource to resources accessible from the directory, or recurses into subdirectories.
52 * Subdirectories are not considered resources, therefore are not added to the list.
53 * @param directory from which resources are added to the list of resources
54 * @param resources list of resources
55 * @param path current path, starting as root from the first recursive call and building as recursion goes.
56 */
57 private void addResourcesFromSubDirectory(File directory, List resources, String path) {
58 if (!directory.isDirectory())
59 throw new IllegalArgumentException("wrong parameter: expecting directory");
60 File[] directoryContent = directory.listFiles();
61 for(int i=0; i < directoryContent.length; ++i) {
62 if (directoryContent[i].isDirectory()) {
63 addResourcesFromSubDirectory(directoryContent[i], resources, path + directoryContent[i].getName() + File.separator);
64 } else {
65 AccessibleResource resource = new AccessibleResourceImpl(directoryContent[i], path, this);
66 resources.add(resource);
67 }
68 }
69 }
70
71 /***
72 * Returns the string representation of the directory resource.
73 * @return the string representation of the directory resource.
74 */
75 public String toString() {
76 StringBuffer sb = new StringBuffer(100);
77 sb.append("DirectoryArchive[");
78 sb.append(super.toString());
79 sb.append("]\n");
80 return sb.toString();
81 }
82
83 /***
84 * Returns the xml representation of the resource.
85 * @return the xml representation of the resource.
86 */
87 public String toXml() {
88 StringBuffer sb = new StringBuffer(100);
89 sb.append("<directoryArchive ");
90 sb.append(super.toString());
91 sb.append("/>\n");
92 return sb.toString();
93 }
94 }